<?php
/**
 * Created by PhpStorm.
 * User: lgm
 * Date: 2018/9/3
 * Time: 19:32
 */

namespace app\admin\controller;

use think\Db;
use think\facade\Request;

class Infocate extends MyController
{
    //资讯分类列表
    public function index()
    {
        $infocate = db('info_category')->select();
        $this->assign(['infocate'=>$infocate]);
        return view();
    }
    //Ajax请求 添加分类 修改分类
    public function cateEdit()
    {
        if (Request::isAjax())
        {
            $data = Request::post();
            if (empty($data['cid'])){
                //添加分类
                $res = db('info_category')
                    ->insert(['cat_title'=>$data['cat_title']]);
                if ($res){echo 1;exit;}else{echo 0;exit;}
            }else{
                //修改分类
                $cid = $data['cid'];
                $res = db('info_category')->where('cat_id',$cid)->update(['cat_title'=>$data['cat_title']]);
                if ($res){echo 1;exit;}else{echo 0;exit;}
            }
        }
    }

    //删除资讯
    public function del()
    {
        $cid = input('cid');
        $row = db('information')->where('cat_id',$cid)->select();
        if ($row){
            $this->error('删除失败，请先删除该分类下的资讯');
        }{
        Db::name('info_category')->where('cat_id',$cid)->delete();
        $this->success('删除成功','Infocate/index');
    }
    }
}
